<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="大赛名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入大赛名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="赛事类别" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择赛事类别" clearable>
          <el-option
            v-for="dict in dict.type.skill_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="大赛阶段" prop="status" v-if="type=='m'">
        <el-select v-model="queryParams.status" placeholder="请选择大赛阶段" clearable>
          <el-option
            v-for="dict in dict.type.skill_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="审核状态" prop="checkStatus" v-if="type=='check'">
        <el-select v-model="queryParams.checkStatus" placeholder="请选择审核状态" clearable>
          <el-option
            v-for="dict in dict.type.check_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8" v-if="type=='m'">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:skill:add']"
        >新增
        </el-button>
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="success"-->
      <!--          plain-->
      <!--          icon="el-icon-edit"-->
      <!--          size="mini"-->
      <!--          :disabled="single"-->
      <!--          @click="handleUpdate"-->
      <!--          v-hasPermi="['system:skill:edit']"-->
      <!--        >修改</el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="danger"-->
      <!--          plain-->
      <!--          icon="el-icon-delete"-->
      <!--          size="mini"-->
      <!--          :disabled="multiple"-->
      <!--          @click="handleDelete"-->
      <!--          v-hasPermi="['system:skill:remove']"-->
      <!--        >删除</el-button>-->
      <!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="skillList" @selection-change="handleSelectionChange">
      <!--      <el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="序号" type="index" width="60" header-align="center" align="center"></el-table-column>
      <el-table-column label="大赛名称" align="center" prop="name" width="200" show-overflow-tooltip/>
      <el-table-column label="大赛阶段" align="center" prop="status"  v-if="type=='m'" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.skill_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="赛事类别" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.skill_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="报名类型" align="center" prop="bmType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bm_type" :value="scope.row.bmType"/>
        </template>
      </el-table-column>
      <el-table-column label="学校名称" align="center" prop="schoolName"/>
      <el-table-column label="参赛对象" align="center" prop="xyDeptName"/>
      <el-table-column label="专家名称" align="center" prop="zjName"/>
      <el-table-column label="待审核数量" align="center" prop="dshCount"  v-if="type=='bmcheck'">
        <template slot-scope="scope">
          <el-tag  v-if="scope.row.dshCount>0"  class="ml-2" type="danger">{{scope.row.dshCount}}</el-tag>
          <el-tag  v-else class="ml-2" type="info">{{scope.row.dshCount}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="报名截止时间" align="center" prop="bmEndDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.bmEndDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="checkStatus" v-if="type=='check'">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.check_status" :value="scope.row.checkStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="审核时间" align="center" prop="checkDate" width="180" v-if="type=='check'">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.checkDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核备注" align="center" prop="checkRemark" v-if="type=='check'"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:skill:edit']"
            v-if="type=='m'"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDetail(scope.row)"
          >查看
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="ruledetail(scope.row)"
            v-if="type=='m'"
          >获奖规则
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="scoredetail(scope.row)"
            v-if="type=='score' || type=='cert'"
          >成绩信息
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="checkUpdate(scope.row)"
            v-if="type=='check'"
          >审核
          </el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="bmCheckdetail(scope.row)"
            v-if="type=='bmcheck'"
          >报名信息
          </el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-if="type=='m'"
            v-hasPermi="['system:skill:remove']"
          >删除
          </el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="pfdetail(scope.row)"
            v-if="type=='pf'"
          >评分
          </el-button>

        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改赛事管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="大赛名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入大赛名称"/>
            </el-form-item>


          </el-col>
          <el-col :span="12">
            <el-form-item label="赛事类别" prop="type">
              <el-select v-model="form.type" placeholder="请选择赛事类别">
                <el-option
                  v-for="dict in dict.type.skill_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="联系人" prop="linkPerson">
              <el-input v-model="form.linkPerson" placeholder="请输入联系人"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="linkPhone">
              <el-input v-model="form.linkPhone" placeholder="请输入联系人"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="报名类型" prop="bmType">
          <el-select v-model="form.bmType" placeholder="请选择报名类型">
            <el-option
              v-for="dict in dict.type.bm_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="图片" prop="image">
          <image-upload v-model="form.image" :limit="1"/>
        </el-form-item>

        <el-row>
          <el-col :span="12">
            <el-form-item label="积分规则" prop="jfRule">
              <el-input v-model="form.jfRule" type="textarea" :rows="5" placeholder="请输入内容"/>
            </el-form-item>

          </el-col>
          <el-col :span="12">
            <el-form-item label="报名条件" prop="bmTj">
              <el-input v-model="form.bmTj" type="textarea" :rows="5" placeholder="请输入内容"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="介绍" prop="projectInfo">
              <el-input v-model="form.projectInfo" type="textarea" :rows="5" placeholder="请输入内容"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">

            <el-form-item label="赛程安排" prop="skillArrange">
              <el-input v-model="form.skillArrange" type="textarea" :rows="5" placeholder="请输入内容"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">

          </el-col>
          <el-col :span="12">

          </el-col>
        </el-row>
        <el-form-item label="专家" prop="zjId">
          <el-select v-model="form.zjId" filterable placeholder="请选择">
            <el-option
              filterable
              v-for="item in userList"
              :key="item.userId"
              :label="item.nickName"
              :value="item.userId"
            >
            </el-option>
          </el-select>

          <el-button
            style="margin-left: 20px"
            size="mini"
            type="primary"
            v-if="title=='修改赛事' ||  '添加赛事'"
            @click="randomselectZj()"
          >随机抽取专家
          </el-button>

        </el-form-item>

        <el-form-item label="比赛地点" prop="address">
          <el-input v-model="form.address" placeholder="请输入比赛地点"/>
        </el-form-item>


        <el-row>
          <el-col :span="12">
            <el-form-item label="报名开始时间" prop="bmStartDate">
              <el-date-picker clearable
                              v-model="form.bmStartDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择报名截止时间"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="报名截止时间" prop="bmEndDate">
              <el-date-picker clearable
                              v-model="form.bmEndDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择报名截止时间"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>


        <el-row>
          <el-col :span="12">
            <el-form-item label="比赛开始时间" prop="bsDate">
              <el-date-picker clearable
                              v-model="form.bsDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择比赛时间"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="比赛结束时间" prop="bsEndDate">
              <el-date-picker clearable
                              v-model="form.bsEndDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择比赛时间"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>


        <el-form-item label="成绩公布时间" prop="scoreDate">
          <el-date-picker clearable
                          v-model="form.scoreDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择成绩公布时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="参赛对象" prop="xyDeptIdList">
          <el-select v-model="form.xyDeptIdList" multiple filterable placeholder="请选择">
            <el-option
              v-for="item in deptList"
              :key="item.deptId"
              :label="item.deptName"
              :value="item.deptId"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <el-form ref="formfj" label-width="120px">
        <el-form-item label="附件信息" prop="ysSm">
          <div>
            <FileUpload
              v-if="open"
              ref="expertRef"
              :disabled="true"
              :bottom-tips="'文件格式必须为\'.docx\',\'.xlsx\',文件大小不超过10M'"
              :businessId="form.id"
              :biz-type="'xqfj001'"
              :limit="5"
              :fileType="['.docx','xlsx']"
              style="margin-left: 20px"
            ></FileUpload>
          </div>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-if="title!='赛事详情'">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <el-dialog :title="title" :visible.sync="checkopen" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="checkrules" label-width="120px">
        <el-form-item label="审核" prop="checkStatus">
          <el-select v-model="form.checkStatus" style="width: 196.4px" placeholder="审核是否通过" clearable>
            <el-option label="通过" value="2"/>
            <el-option label="不通过" value="3"/>
          </el-select>
        </el-form-item>
        <div class="itemcol textarea">
          <el-form-item label="审核意见">
            <el-input v-model="form.checkRemark" placeholder="审核意见" type="textarea"></el-input>
          </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="checkSubmitForm">确 定</el-button>
        <el-button @click="checkCancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="title" :visible.sync="bmopen" width="1200px" append-to-body>
      <SkillBm :skillId="skillId" :type="type" v-if="bmopen"></SkillBm>
    </el-dialog>
    <el-dialog :title="title" :visible.sync="ruleopen" width="1200px" append-to-body>
      <Rule :skillId="skillId" v-if="ruleopen"></Rule>
    </el-dialog>

  </div>
</template>

<script>
import { listSkill, getSkill, delSkill, addSkill, updateSkill, check,saveOrUpdate } from '@/api/business/skill'
import { getUserlistByRole } from '@/api/system/user'
import { getDeptlist } from '@/api/system/dept'
import FileUpload from '@/components/fileUpload_new.vue'

import SkillBm from '@/components/skillbm/index'
import Rule from '@/components/rule/index'
import { guid } from '../../utils/utils'

export default {
  name: 'SkillZJ',
  dicts: ['check_status', 'skill_type', 'bm_type','skill_status'],
  components: { FileUpload, SkillBm, Rule },
  props: {
    type: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      skillId: '',
      // 总条数
      total: 0,
      // 赛事管理表格数据
      skillList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      checkopen: false,
      bmopen: false,
      ruleopen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        jfRule: null,
        bmTj: null,
        projectInfo: null,
        skillArrange: null,
        type: null,
        zjId: null,
        image: null,
        address: null,
        bmEndDate: null,
        bsDate: null,
        scoreDate: null,
        xyDeptId: null,
        xyDeptIdList: null,
        sbUserId: null,
        checkStatus: null,
        checkDate: null,
        checkRemark: null,
        status: null,
      },
      // 表单参数
      form: {},
      userList: [],
      deptList: [],

      // 表单校验
      rules: {
        name: [
          { required: true, message: '赛事名称不能为空', trigger: 'blur' }
        ],
        linkPerson: [
          { required: true, message: '联系人不能为空', trigger: 'blur' }
        ],
        linkPhone: [
          { required: true, message: '联系电话不能为空', trigger: 'blur' }
        ],
        jfRule: [
          { required: true, message: '积分规则不能为空', trigger: 'blur' }
        ],
        bmTj: [
          { required: true, message: '报名条件不能为空', trigger: 'blur' }
        ],
        projectInfo: [
          { required: true, message: '项目信息不能为空', trigger: 'blur' }
        ],
        skillArrange: [
          { required: true, message: '赛事安排不能为空', trigger: 'blur' }
        ],
        bmType: [
          { required: true, message: '报名类型不能为空', trigger: 'blur' }
        ],
        type: [
          { required: true, message: '类型不能为空', trigger: 'blur' }
        ],
        zjId: [
          { required: true, message: '专家不能为空', trigger: 'blur' }
        ],
        image: [
          { required: true, message: '图片不能为空', trigger: 'blur' }
        ],
        address: [
          { required: true, message: '地址不能为空', trigger: 'blur' }
        ],
        bmStartDate: [
          { required: true, message: '报名开始时间不能为空', trigger: 'blur' }
        ],
        bmEndDate: [
          { required: true, message: '报名截止时间不能为空', trigger: 'blur' }
        ],
        bsDate: [
          { required: true, message: '比赛开始时间不能为空', trigger: 'blur' }
        ],
        bsEndDate: [
          { required: true, message: '比赛截止不能为空', trigger: 'blur' }
        ],

        scoreDate: [
          { required: true, message: '打分时间不能为空', trigger: 'blur' }
        ],
        xyDeptIdList: [
          { required: true, message: '参赛对象不能为空', trigger: 'blur' }
        ],

      },
      checkrules: {
        checkStatus: [
          { required: true, message: '是否通过', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
    this.getUserlistByRole()
    this.getDeptlist()
  },
  methods: {
    /** 查询赛事管理列表 */
    getList() {
      this.loading = true
      if (this.type == 'pf' || this.type == 'm'  || this.type == 'bmcheck'  || this.type == 'cert'  || this.type == 'score' ) {
        this.queryParams.checkStatus = '2'
      }
      listSkill(this.queryParams).then(response => {
        this.skillList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    getUserlistByRole() {
      var data = {
        roleKey: 'system:zj'
      }
      getUserlistByRole(data).then(response => {
        this.userList = response.data
      })
    },
    randomselectZj(){
      // 从       this.userList 中随机抽取一个用户
      var index = Math.floor(Math.random() * this.userList.length)
      this.form.zjId = this.userList[index].userId
    },
    getDeptlist() {
      var data = {
        parentId: 100
      }
      getDeptlist(data).then(response => {
        this.deptList = response.data
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    checkCancel() {
      this.checkopen = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        jfRule: null,
        bmTj: null,
        projectInfo: null,
        skillArrange: null,
        type: null,
        zjId: null,
        image: null,
        address: null,
        bmEndDate: null,
        bsDate: null,
        bmStartDate: null,
        bsEndDate: null,
        scoreDate: null,
        xyDeptId: null,
        sbUserId: null,
        checkStatus: null,
        checkDate: null,
        createBy: null,
        checkRemark: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        delFlag: null,
        bmType: null,
        linkPerson: null,
        linkPhone: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加赛事'
      let mizId = guid().replace(/-/g, '')
      this.form.id = mizId
    },
    checkUpdate(row) {
      this.form.checkStatus = '2'
      this.form.id = row.id
      this.checkopen = true
      this.title = '审核'

    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getSkill(id).then(response => {
        this.getDeptlist()
        this.form = response.data
        this.open = true
        this.title = '修改赛事'
        if (this.form.zjId) {
          this.form.zjId = Number(this.form.zjId)
        }
        if (this.form.xyDeptId) {
          this.$set(this.form, 'xyDeptIdList', [])
          // list 的每一个元素 转换为数字
          this.form.xyDeptIdList = this.form.xyDeptId.split(',').map(Number)
        }

      })
    },

    handleDetail(row) {
      this.reset()
      const id = row.id || this.ids
      getSkill(id).then(response => {
        this.getDeptlist()
        this.form = response.data
        this.open = true
        this.title = '赛事详情'
        if (this.form.zjId) {
          this.form.zjId = Number(this.form.zjId)
        }
        if (this.form.xyDeptId) {
          this.$set(this.form, 'xyDeptIdList', [])
          // list 的每一个元素 转换为数字
          this.form.xyDeptIdList = this.form.xyDeptId.split(',').map(Number)
        }

      })
    },

    checkSubmitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          check(this.form).then(response => {
            this.$modal.msgSuccess('修改成功')
            this.checkopen = false
            this.getList()
          })
        }
      })
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          // 判断时间的合法性
          if (this.form.bmStartDate > this.form.bmEndDate) {
            this.$modal.msgError('报名开始时间不能大于报名结束时间')
            return
          }
          if (this.form.bsDate < this.form.bmEndDate) {
            this.$modal.msgError('比赛开始时间不能小于报名结束时间')
            return
          }
          if (this.form.bsDate > this.form.bsEndDate) {
            this.$modal.msgError('比赛开始时间不能大于比赛结束时间')
            return
          }
          if (this.form.scoreDate < this.form.bsEndDate) {
            this.$modal.msgError('成绩公布时间不能小于比赛结束时间')
            return
          }

          this.form.xyDeptId = this.form.xyDeptIdList.join(',')
            saveOrUpdate(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
        }
      })
    },
    pfdetail(row) {
      this.bmopen = true
      this.skillId = row.id
    },
    bmCheckdetail(row) {
      this.bmopen = true
      this.skillId = row.id
    },
    ruledetail(row) {
      this.ruleopen = true
      this.skillId = row.id
    },
    scoredetail(row) {
      this.bmopen = true
      this.skillId = row.id
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除赛事编号为"' + ids + '"的数据项？').then(function() {
        return delSkill(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/skillBmUser/export', {
        ...this.queryParams
      }, `skill_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
